package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.atguigu.tingshu.vo.album.UploadTrackVo;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Slf4j
@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private VodConstantProperties vodConstantProperties;
    @Autowired
    private VodUploadClient client;
    @Autowired
    private TrackInfoMapper trackInfoMapper;

    @Override
    public UploadTrackVo uploadTrack(MultipartFile file) {
        /**
         * SecretId: AKIDSuTGJTjBbF uT9lXQ1WjsDa3T2iN7scGq
         * SecretKey: dAZvHe2PrOoCd7NpRC1yutu1EvdKRiCk
         */
        //把上传的文件数据保存到磁盘自定目录下，形成临时文件
        String uploadTempPath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);


        //创建上传请求对象
        VodUploadRequest request = new VodUploadRequest();
        request.setMediaFilePath(uploadTempPath);
        request.setProcedure(vodConstantProperties.getProcedure());//设置任务流名称
        //调用上传
        try {
            VodUploadResponse response = client.upload(vodConstantProperties.getRegion(),request);
            //构建响应结果
            UploadTrackVo uploadTrackVo = new UploadTrackVo();
            uploadTrackVo.setMediaFileId(response.getFileId());
            uploadTrackVo.setMediaUrl(response.getMediaUrl());
                        //返回
            return uploadTrackVo;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            new File(uploadTempPath).delete();  //删除临时文件
        }
        return new UploadTrackVo();
    }

    @Override
    public TrackMediaInfoVo getMediaInfo(String mediaFileId) {
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(vodConstantProperties.getSecretId(),vodConstantProperties.getSecretKey());
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("vod.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        VodClient client = new VodClient(cred, vodConstantProperties.getRegion(), clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeMediaInfosRequest req = new DescribeMediaInfosRequest();
        String[] fileIds1 = {mediaFileId};
        req.setFileIds(fileIds1);

        // 返回的resp是一个DescribeMediaInfosResponse的实例，与请求对象对应
        try {
            DescribeMediaInfosResponse resp = client.DescribeMediaInfos(req);
            MediaInfo[] mediaInfoSet = resp.getMediaInfoSet();
            MediaInfo mediaInfo = mediaInfoSet[0];
            MediaMetaData metaData = mediaInfo.getMetaData();
            MediaBasicInfo basicInfo = mediaInfo.getBasicInfo();
            //从DescribeMediaInfosResponse对象中获取媒体属性数据，将其封装到TrackMediaInfoVo对象
            TrackMediaInfoVo trackMediaInfoVo = new TrackMediaInfoVo();
            trackMediaInfoVo.setSize(metaData.getSize());
            trackMediaInfoVo.setDuration(metaData.getDuration());
            trackMediaInfoVo.setMediaUrl(basicInfo.getMediaUrl());
            trackMediaInfoVo.setType(basicInfo.getType());

            return trackMediaInfoVo;
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return new TrackMediaInfoVo();
    }

    @Override
    public void removeMediaFile(String mediaFileId) {
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        Credential cred = new Credential(vodConstantProperties.getSecretId(), vodConstantProperties.getSecretKey());
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("vod.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        VodClient client = new VodClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DeleteMediaRequest req = new DeleteMediaRequest();
        req.setFileId(mediaFileId);
        // 返回的resp是一个DeleteMediaResponse的实例，与请求对象对应
        DeleteMediaResponse resp = null;
        try {
            resp = client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        log.info("delete medial response:{}", JSON.toJSONString(resp));
    }
}
